Public Class PurchaseOrder
    Private WithEvents _aMetaDataTableChild As CommonExt.MetaDataTable
    Private WithEvents _aMetaDataTable As CommonExt.MetaDataTable
    Private WithEvents _aDataset As New DataSet("ReportDataSet")
    Private _aQueryName As String

    Private Sub PurchaseOrder_BeforePrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeforePrint



        'Get MetaData by QueryName
        _aQueryName = "RptPO"
        Dim _aQueryNameChild As String = "RptPODetail"
        Dim mq As MetaQuery = MetaQuery.GetMetaQueryByName(_aQueryName)
        Dim mqChild As MetaQuery = MetaQuery.GetMetaQueryByName(_aQueryNameChild)

        'Set Where Clause to add Parameters Value
        mq.SQLWhere = "PurchaseOrderCD = '" + Me.Parameters("PO").Value.ToString() + "'"
        mq.FillResultQuery()

        mqChild.SQLWhere = "PurchaseOrderCD = '" + Me.Parameters("PO").Value.ToString() + "'"
        mqChild.FillResultQuery()


        'Add RptPO & RptPODetail DataTables to Dataset
        _aDataset = Nothing
        _aDataset = New DataSet("ReportDataSet")
        _aDataset.Tables.Add(mq.ResultQuery)
        _aDataset.Tables.Add(mqChild.ResultQuery)


        'Add DataRelation between RptPO & RptPODetail
        Dim aParentRelationField As String = mq.GUIDField
        Dim aChildRelationField As String = mq.GUIDField + "_FK"
        Dim aRelation As New DataRelation("RptPO/RptPODetail", mq.ResultQuery.Columns(aParentRelationField), mqChild.ResultQuery.Columns(aChildRelationField))
        _aDataset.Relations.Add(aRelation)


        Dim b As XtraReports.UI.XRBinding

        'Bind Report Controls
        For Each aCol As DataColumn In mq.ResultQuery.Columns
            b = New XtraReports.UI.XRBinding("Text", mq.ResultQuery, aCol.ColumnName)
            Dim aControl As XtraReports.UI.XRTableCell = CType(Me.FindControl(aCol.ColumnName, True), XtraReports.UI.XRTableCell)

            If Not aControl Is Nothing Then
                Try
                    aControl.DataBindings.Add(b)
                Catch ex As Exception
                End Try
            End If
        Next


        'Bind Sub Report Controls
        For Each aCol As DataColumn In mqChild.ResultQuery.Columns
            b = New XtraReports.UI.XRBinding("Text", mqChild.ResultQuery, aCol.ColumnName)
            Dim aControl As XtraReports.UI.XRTableCell = CType(Me.FindControl(aCol.ColumnName, True), XtraReports.UI.XRTableCell)

            If Not aControl Is Nothing Then
                Try
                    aControl.DataBindings.Add(b)
                Catch ex As Exception
                End Try
            End If
        Next


        Me.DataSource = _aDataset
        Me.DataMember = _aDataset.Tables(0).TableName

        Me.DetailReport.DataSource = _aDataset
        Me.DetailReport.DataMember = _aDataset.Tables(1).TableName


    End Sub

End Class